package com.example.test.utils;

import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtils {
    public static boolean uploadFile(byte[] aryFileBytes, String filePath) {
        boolean bRet = true;
        try {
            File classPathDirectory = null;
            classPathDirectory = new File(ResourceUtils.getURL("classpath:").getPath());
            if (!classPathDirectory.exists()) {
                classPathDirectory = new File("");
            }
            File uploadDirectory = new File(classPathDirectory.getAbsolutePath(), filePath.substring(0,filePath.lastIndexOf("/")+1));
            if (uploadDirectory.exists() == Boolean.FALSE) {
                uploadDirectory.mkdirs();
            }
            FileOutputStream out = new FileOutputStream(classPathDirectory.getAbsolutePath()+"/"+filePath);
            out.write(aryFileBytes);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            bRet = false;
        }
        return bRet;
    }
}
